package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceShadowVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理Controller
 *
 * @author JQ
 * @date 2025-08-03
 */
@Tag(name = "设备管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /***
     * 查询设备影子数据（设备详情）
     */
    @Operation(summary = "查询设备影子数据（设备详情）")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<DeviceShadowVo>> queryServiceProperties(@PathVariable("iotId")String iotId){
        List<DeviceShadowVo> list = deviceService.queryServiceProperties(iotId);
        return R.ok(list);
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @Operation(summary = "获取设备详细信息")
    public AjaxResult getInfo(@Parameter(description = "物联网设备id") @PathVariable("iotId") String iotId) {

        return success(deviceService.getDeviceDetail(iotId));
    }

    /**
     * 查询设备管理列表
     */
    @PostMapping("/register")
    @Operation(summary = "设备注册")
    public AjaxResult register(@RequestBody DeviceDto dto) {

        deviceService.registerDevice(dto);
        return success();
    }

    /**
     * 从物联网平台同步产品列表
     */
    @PostMapping("/syncProductList")
    @Operation(summary = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    @GetMapping("/allProduct")
    @Operation(summary = "查询所有项目")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }

    /**
     * 查询设备管理列表
     */
    @Operation(summary = "查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device) {

        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @Operation(summary = "导出设备管理列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 新增设备管理
     */
    @Operation(summary = "新增设备管理")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device) {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备管理
     */
    @Operation(summary = "修改设备管理")
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return R.ok();
    }

    /**
     * 删除设备管理
     */
    @Operation(summary = "删除设备")
    @DeleteMapping("/{iotId}")
    public R deleteDevice(@PathVariable String iotId) {
        deviceService.deleteDevice(iotId);
        return R.ok();
    }


    /**
     * 查询设备信息(为报警模块做准备)
     */
    @GetMapping("/queryProduct/{productKey}")
    @Operation(summary = "查询产品详情")
    public R<List<ServiceCapability>> queryProduct(@PathVariable String productKey) {
        return R.ok(deviceService.queryProduct(productKey));
    }

    /**
     * 根据设备id绑定的老人信息查询护理员查询 nursingId集合
     */
    @Operation(summary = "根据设备id绑定的老人信息查询护理员查询 nursingId集合")
    @GetMapping("/selectNursingIdsByIotIdWithElder/{iotId}")
    public R<List<Long>> selectNursingIdsByIotIdWithElder(String iotId){
        return R.ok(deviceService.selectNursingIdsByIotIdWithElder(iotId));
    }

    /**
     * 根据设备id绑定的床位信息查询护理员查询 nursingId集合
     */
    @Operation(summary = "根据设备id绑定的床位信息查询护理员查询 nursingId集合")
    @GetMapping("/selectNursingIdsByIotIdWithBed/{iotId}")
    public R<List<Long>> selectNursingIdsByIotIdWithBed(String iotId){
        return R.ok(deviceService.selectNursingIdsByIotIdWithBed(iotId));
    }
}
